﻿using System;
using System.Threading;
using KJFramework.Net.Channels.Enums;
using KJFramework.ServiceModel.Bussiness.Default.Proxy;
using KJFramework.ServiceModel.Bussiness.Default.Services;
using KJFramework.ServiceModel.Elements;
using KJFramework.ServiceModel.Objects;
using KJFramework.ServiceModel.Proxy;
using KJFramework.Timer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ServerTest;
using ServerTest.Contract;
using IHelloWorld = ClientTest.IHelloWorld;

namespace KJFramework.ServiceModel.UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void BigDataTransferTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/BigDataTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            //client area.
           IClientProxy<ITestContract> proxy = new DefaultClientProxy<ITestContract>(new TcpBinding("tcp://localhost:9999/BigDataTest"));
            CodeTimer.Initialize();
            string content = null;
            CodeTimer.Time("Big data test", 1, delegate
            {
                content = proxy.Channel.BigData("Yeah, that's a big data transfer test!");
                Assert.IsNotNull(content);
            });
            Thread.Sleep(2000);
            Console.WriteLine(content);
            serviceHost.Close();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Closed);
        }

        [TestMethod]
        public void NormalCallTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            //client area.
            IClientProxy<ITestContract> proxy = new DefaultClientProxy<ITestContract>(new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            CodeTimer.Initialize();
            string content = null;
            CodeTimer.Time("Big data test", 1, delegate
            {
                content = proxy.Channel.NormalCall("Yeah, that's a normal call test!");
                Assert.IsNotNull(content);
            });
            Thread.Sleep(2000);
            Assert.IsTrue(content == "Yes!~");
            serviceHost.Close();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Closed);
        }

        [TestMethod]
        public void VoidCallTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            //client area.
            IClientProxy<ITestContract> proxy = new DefaultClientProxy<ITestContract>(new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            CodeTimer.Initialize();
            CodeTimer.Time("Void call test", 1, delegate
            {
                proxy.Channel.VoidCall("Yeah, that's a normal call test!");
            });
            serviceHost.Close();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Closed);
        }

        [TestMethod]
        public void GetBigArrayRankTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            //client area.
            IClientProxy<ITestContract> proxy = new DefaultClientProxy<ITestContract>(new TcpBinding("tcp://localhost:9999/NormalCallTest"));
            CodeTimer.Initialize();
            int[] content = null;
            CodeTimer.Time("Big data test", 1, delegate
            {
                content = proxy.Channel.GetBigArrayRank();
                Assert.IsNotNull(content);
            });
            Thread.Sleep(5000);
            Assert.IsTrue(content.Length == 8888);
            Assert.IsTrue(content[8887] == 88);
            serviceHost.Close();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Closed);
        }

        [TestMethod]
        public void ServiceContractNormalTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/Test"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            //string address = "tcp://10.10.36.168:9999/Test";
            string address = "tcp://localhost:9999/Test";
            IClientProxy<IHelloWorld> instance0 = new DefaultClientProxy<IHelloWorld>(new TcpBinding(address));
            InObject inObject = new InObject { Inf1 = 1, Info2 = DateTime.Now, Info = "Info", Info3 = new[] { "1", "2" }, Info4 = new[] { 3, 4 } };
            for (int i = 0; i < 100; i++)
            {
                instance0.Channel.TestIntellectObjectAsync(inObject, delegate(IAsyncCallResult callResult)
                {
                    Assert.IsTrue(callResult.IsSuccess);
                    Assert.IsNotNull(callResult.GetResult<string>());
                });
            }
        }


        [TestMethod]
        public void PerformanceServiceContractNormalTest()
        {
            string address = "tcp://localhost:9999/Test";
            IClientProxy<IHelloWorld> instance0 = new DefaultClientProxy<IHelloWorld>(new TcpBinding(address));
            InObject inObject = new InObject { Inf1 = 1, Info2 = DateTime.Now, Info = "Info", Info3 = new[] { "1", "2" }, Info4 = new[] { 3, 4 } };
            instance0.Channel.TestIntellectObjectAsync(inObject, delegate(IAsyncCallResult callResult)
            {
                Assert.IsTrue(callResult.IsSuccess);
                Assert.IsNotNull(callResult.GetResult<string>());
            });
        }


        [TestMethod]
        public void AsyncCallTest()
        {
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            ServiceHost serviceHost = new ServiceHost(typeof(HellowWorld), new TcpBinding("tcp://localhost:9999/AsyncCalllTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            IClientProxy<IHelloWorld> instance0 = new DefaultClientProxy<IHelloWorld>(new TcpBinding("tcp://localhost:9999/AsyncCalllTest"));
            InObject inObject = new InObject { Inf1 = 1, Info2 = DateTime.Now, Info = "Info", Info3 = new[] { "1", "2" }, Info4 = new[] { 3, 4 } };
            instance0.Channel.TestIntellectObjectAsync(inObject, delegate(IAsyncCallResult callResult)
            {
                Assert.IsTrue(callResult.IsSuccess);
                string result = callResult.GetResult<string>();
                Assert.IsNotNull(result);
                Assert.IsTrue(result == "12345678");
                autoResetEvent.Set();
            });
            Assert.IsTrue(autoResetEvent.WaitOne(5000));
        }

        [TestMethod]
        public void AsyncCallWithNoArgsTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(HellowWorld), new TcpBinding("tcp://localhost:9999/AsyncCalllTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            IClientProxy<IHelloWorld> instance0 = new DefaultClientProxy<IHelloWorld>(new TcpBinding("tcp://localhost:9999/AsyncCalllTest"));
            instance0.Channel.TestIntellectObjectAsync();
        }

        [TestMethod]
        public void OnewayCallTest()
        {
            ServiceHost serviceHost = new ServiceHost(typeof(TestContract), new TcpBinding("tcp://localhost:9999/OnewayCalllTest"));
            serviceHost.IsSupportExchange = false;
            serviceHost.Open();
            Assert.IsTrue(serviceHost.CommunicationState == CommunicationStates.Opened);
            IClientProxy<ITestContract> instance0 = new DefaultClientProxy<ITestContract>(new TcpBinding("tcp://localhost:9999/OnewayCalllTest"));
            instance0.Channel.OnewayCall();
            Thread.Sleep(500);
        }
    }
}
